<?php
/**
 * WordPress Administration Scheme API
 *
 * Here we keep the DB structure and option values.
 *
 * @see wp-admin/includes/schema.php
 */

/**
 * Retrieve the SQL for creating database tables.
 *
 * @since 3.3.0
 *
 * @param string $scope Optional. The tables for which to retrieve SQL. Can be all, global, ms_global, or blog tables. Defaults to all.
 * @param int $blog_id Optional. The blog ID for which to retrieve SQL. Default is the current blog ID.
 * @return string The SQL needed to create the requested tables.
 */
function wp_get_db_sqlite_schema( $scope = 'all', $blog_id = null ) {
	global $wpdb;
	$charset_collate = '';

	if ( $blog_id && $blog_id != $wpdb->blogid )
		$old_blog_id = $wpdb->set_blog_id( $blog_id );

	// Engage multisite if in the middle of turning it on from network.php.
	$is_multisite = is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK );

	// Blog specific tables.
	$blog_tables = "
CREATE TABLE $wpdb->terms (
 term_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
 name varchar(200) NOT NULL default '',
 slug varchar(200) NOT NULL default '',
 term_group bigint(10) NOT NULL default 0,
-- PRIMARY KEY  (term_id),
 UNIQUE (slug) --,
-- KEY name (name)
) $charset_collate;
--  create index name on $wpdb->terms(name);

CREATE TABLE $wpdb->term_taxonomy (
 term_taxonomy_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
 term_id INTEGER(8) NOT NULL default 0,
 taxonomy varchar(32) NOT NULL default '',
 description longtext NOT NULL,
 parent INTEGER(8) NOT NULL default 0,
 count INTEGER(8) NOT NULL default 0,
-- PRIMARY KEY  (term_taxonomy_id),
 UNIQUE (term_id,taxonomy) --,
-- KEY taxonomy (taxonomy)
) $charset_collate;
--  create index taxonomy on $wpdb->term_taxonomy(taxonomy);

CREATE TABLE $wpdb->term_relationships (
 object_id INTEGER(8) NOT NULL default 0,
 term_taxonomy_id INTEGER(8) NOT NULL default 0,
 term_order int(11) NOT NULL default 0,
 PRIMARY KEY  (object_id,term_taxonomy_id) --,
-- KEY term_taxonomy_id (term_taxonomy_id)
) $charset_collate;
--  create index term_taxonomy_id on $wpdb->term_taxonomy(term_taxonomy_id);

CREATE TABLE $wpdb->commentmeta (
  meta_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  comment_id INTEGER(8) NOT NULL default '0',
  meta_key varchar(255) default NULL,
  meta_value longtext --,
--  PRIMARY KEY  (meta_id),
--  KEY comment_id (comment_id),
--  KEY meta_key (meta_key)
) $charset_collate;
--  create index comment_id on $wpdb->commentmeta(comment_id);
--  create index meta_key on $wpdb->commentmeta(meta_key);

CREATE TABLE $wpdb->comments (
  comment_ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  comment_post_ID INTEGER(8) NOT NULL default '0',
  comment_author tinytext NOT NULL,
  comment_author_email varchar(100) NOT NULL default '',
  comment_author_url varchar(200) NOT NULL default '',
  comment_author_IP varchar(100) NOT NULL default '',
  comment_date datetime NOT NULL default '0000-00-00 00:00:00',
  comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  comment_content text NOT NULL,
  comment_karma int(11) NOT NULL default '0',
  comment_approved varchar(20) NOT NULL default '1',
  comment_agent varchar(255) NOT NULL default '',
  comment_type varchar(20) NOT NULL default '',
  comment_parent INTEGER(8) NOT NULL default '0',
  user_id INTEGER(8) NOT NULL default '0' --,
--  PRIMARY KEY  (comment_ID),
--  KEY comment_post_ID (comment_post_ID),
--  KEY comment_approved_date_gmt (comment_approved,comment_date_gmt),
--  KEY comment_date_gmt (comment_date_gmt),
--  KEY comment_parent (comment_parent)
) $charset_collate;
--  create index comment_post_ID on $wpdb->comments(comment_post_ID);
--  create index comment_approved on $wpdb->comments(comment_approved,comment_date_gmt);
--  create index comment_date_gmt on $wpdb->comments(comment_date_gmt);
--  create index comment_parent on $wpdb->comments(comment_parent);

CREATE TABLE $wpdb->links (
  link_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  link_url varchar(255) NOT NULL default '',
  link_name varchar(255) NOT NULL default '',
  link_image varchar(255) NOT NULL default '',
  link_target varchar(25) NOT NULL default '',
  link_description varchar(255) NOT NULL default '',
  link_visible varchar(20) NOT NULL default 'Y',
  link_owner INTEGER(8) NOT NULL default '1',
  link_rating int(11) NOT NULL default '0',
  link_updated datetime NOT NULL default '0000-00-00 00:00:00',
  link_rel varchar(255) NOT NULL default '',
  link_notes mediumtext NOT NULL,
  link_rss varchar(255) NOT NULL default '' --,
--  PRIMARY KEY  (link_id),
--  KEY link_visible (link_visible)
) $charset_collate;
--  create index link_visible on $wpdb->links(link_visible);

CREATE TABLE $wpdb->options (
  option_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  option_name varchar(64) NOT NULL default '',
  option_value longtext NOT NULL,
  autoload varchar(20) NOT NULL default 'yes',
--  PRIMARY KEY  (option_id),
  UNIQUE (option_name)
) $charset_collate;

CREATE TABLE $wpdb->postmeta (
  meta_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  post_id INTEGER(8) NOT NULL default '0',
  meta_key varchar(255) default NULL,
  meta_value longtext --,
--  PRIMARY KEY  (meta_id),
--  KEY post_id (post_id),
--  KEY meta_key (meta_key)
) $charset_collate;
--  create index post_id on $wpdb->postmeta(post_id);
--  create index meta_key on $wpdb->postmeta(meta_key);

CREATE TABLE $wpdb->posts (
  ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  post_author INTEGER(8) NOT NULL default '0',
  post_date datetime NOT NULL default '0000-00-00 00:00:00',
  post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  post_content longtext NOT NULL,
  post_title text NOT NULL,
  post_excerpt text NOT NULL,
  post_status varchar(20) NOT NULL default 'publish',
  comment_status varchar(20) NOT NULL default 'open',
  ping_status varchar(20) NOT NULL default 'open',
  post_password varchar(20) NOT NULL default '',
  post_name varchar(200) NOT NULL default '',
  to_ping text NOT NULL,
  pinged text NOT NULL,
  post_modified datetime NOT NULL default '0000-00-00 00:00:00',
  post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  post_content_filtered longtext NOT NULL,
  post_parent INTEGER(8) NOT NULL default '0',
  guid varchar(255) NOT NULL default '',
  menu_order int(11) NOT NULL default '0',
  post_type varchar(20) NOT NULL default 'post',
  post_mime_type varchar(100) NOT NULL default '',
  comment_count INTEGER(8) NOT NULL default '0' --,
--  PRIMARY KEY  (ID),
--  KEY post_name (post_name),
--  KEY type_status_date (post_type,post_status,post_date,ID),
--  KEY post_parent (post_parent),
--  KEY post_author (post_author)
) $charset_collate;
--  create index post_name on $wpdb->posts(post_name);
--  create index type_status_date on $wpdb->posts(post_type,post_status,post_date,ID);
--  create index post_parent on $wpdb->posts(post_parent);
--  create index post_author on $wpdb->posts(post_author);
\n";

	// Single site users table. The multisite flavor of the users table is handled below.
	$users_single_table = "CREATE TABLE $wpdb->users (
  ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  user_login varchar(60) NOT NULL default '',
  user_pass varchar(64) NOT NULL default '',
  user_nicename varchar(50) NOT NULL default '',
  user_email varchar(100) NOT NULL default '',
  user_url varchar(100) NOT NULL default '',
  user_registered datetime NOT NULL default '0000-00-00 00:00:00',
  user_activation_key varchar(60) NOT NULL default '',
  user_status int(11) NOT NULL default '0',
  display_name varchar(250) NOT NULL default '' --,
--  PRIMARY KEY  (ID),
--  KEY user_login_key (user_login),
--  KEY user_nicename (user_nicename)
) $charset_collate;
--  create index user_login on $wpdb->users(user_login);
--  create index user_nicename on $wpdb->users(user_nicename);
\n";

	// Multisite users table
	$users_multi_table = "CREATE TABLE $wpdb->users (
  ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  user_login varchar(60) NOT NULL default '',
  user_pass varchar(64) NOT NULL default '',
  user_nicename varchar(50) NOT NULL default '',
  user_email varchar(100) NOT NULL default '',
  user_url varchar(100) NOT NULL default '',
  user_registered datetime NOT NULL default '0000-00-00 00:00:00',
  user_activation_key varchar(60) NOT NULL default '',
  user_status int(11) NOT NULL default '0',
  display_name varchar(250) NOT NULL default '',
  spam INTEGER(2) NOT NULL default '0',
  deleted INTEGER(2) NOT NULL default '0' --,
--  PRIMARY KEY  (ID),
--  KEY user_login_key (user_login),
--  KEY user_nicename (user_nicename)
) $charset_collate;
--  create index user_login on $wpdb->users(user_login);
--  create index user_nicename on $wpdb->users(user_nicename);
\n";

	// usermeta
	$usermeta_table = "CREATE TABLE $wpdb->usermeta (
  umeta_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  user_id INTEGER(8) NOT NULL default '0',
  meta_key varchar(255) default NULL,
  meta_value longtext --,
--  PRIMARY KEY  (umeta_id),
--  KEY user_id (user_id),
--  KEY meta_key (meta_key)
) $charset_collate;
--  create index user_id on $wpdb->usermeta(user_id);
--  create index meta_key on $wpdb->usermeta(meta_key);
\n";

	// Global tables
	if ( $is_multisite )
		$global_tables = $users_multi_table . $usermeta_table;
	else
		$global_tables = $users_single_table . $usermeta_table;

	// Multisite global tables.
	$ms_global_tables = "CREATE TABLE $wpdb->blogs (
  blog_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  site_id INTEGER(8) NOT NULL default '0',
  domain varchar(200) NOT NULL default '',
  path varchar(100) NOT NULL default '',
  registered datetime NOT NULL default '0000-00-00 00:00:00',
  last_updated datetime NOT NULL default '0000-00-00 00:00:00',
  public INTEGER(2) NOT NULL default '1',
  archived INTEGER(1) NOT NULL default '0',
  mature INTEGER(2) NOT NULL default '0',
  spam INTEGER(2) NOT NULL default '0',
  deleted INTEGER(2) NOT NULL default '0',
  lang_id int(11) NOT NULL default '0' --,
--  PRIMARY KEY  (blog_id),
--  KEY domain (domain(50),path(5)),
--  KEY lang_id (lang_id)
) $charset_collate;
--  create index domain on $wpdb->blogs(domain,path);
--  create index lang_id on $wpdb->blogs(lang_id);

CREATE TABLE $wpdb->blog_versions (
  blog_id INTEGER(8) NOT NULL default '0',
  db_version varchar(20) NOT NULL default '',
  last_updated datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (blog_id) --,
--  KEY db_version (db_version)
) $charset_collate;
--  create index db_version on $wpdb->blog_versions(db_version);

CREATE TABLE $wpdb->registration_log (
  ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  email varchar(255) NOT NULL default '',
  IP varchar(30) NOT NULL default '',
  blog_id INTEGER(8) NOT NULL default '0',
  date_registered datetime NOT NULL default '0000-00-00 00:00:00' --,
--  PRIMARY KEY  (ID),
--  KEY IP (IP)
) $charset_collate;
--  create index IP on $wpdb->registration_log(IP);

CREATE TABLE $wpdb->site (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  domain varchar(200) NOT NULL default '',
  path varchar(100) NOT NULL default '' --,
--  PRIMARY KEY  (id),
--  KEY domain (domain,path)
) $charset_collate;
--  create index domain on $wpdb->site(domain,path);

CREATE TABLE $wpdb->sitemeta (
  meta_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  site_id INTEGER(8) NOT NULL default '0',
  meta_key varchar(255) default NULL,
  meta_value longtext --,
--  PRIMARY KEY  (meta_id),
--  KEY meta_key (meta_key),
--  KEY site_id (site_id)
) $charset_collate;
--  create index meta_key on $wpdb->sitemeta(meta_key);
--  create index site_id on $wpdb->sitemeta(site_id);

CREATE TABLE $wpdb->signups (
  domain varchar(200) NOT NULL default '',
  path varchar(100) NOT NULL default '',
  title longtext NOT NULL,
  user_login varchar(60) NOT NULL default '',
  user_email varchar(100) NOT NULL default '',
  registered datetime NOT NULL default '0000-00-00 00:00:00',
  activated datetime NOT NULL default '0000-00-00 00:00:00',
  active INTEGER(1) NOT NULL default '0',
  activation_key varchar(50) NOT NULL default '',
  meta longtext --,
--  KEY activation_key (activation_key),
--  KEY domain (domain)
) $charset_collate;
--  create index activation_key on $wpdb->signups(activation_key);
--  create index domain on $wpdb->signups(domain);
";

	switch ( $scope ) {
		case 'blog' :
			$queries = $blog_tables;
			break;
		case 'global' :
			$queries = $global_tables;
			if ( $is_multisite )
				$queries .= $ms_global_tables;
			break;
		case 'ms_global' :
			$queries = $ms_global_tables;
			break;
		default:
		case 'all' :
			$queries = $global_tables . $blog_tables;
			if ( $is_multisite )
				$queries .= $ms_global_tables;
			break;
	}

	if ( isset( $old_blog_id ) )
		$wpdb->set_blog_id( $old_blog_id );

	return $queries;
}
